package com.happystreet.loader
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 *队列加载器单元 
	 * @author Liseen
	 * 
	 */	
	public class LoadingQueueItem
	{
		public static const ITEM_TYPE_SWF:String = "swf";
		public static const ITEM_TYPE_XML:String = "xml";
		
		//目标URL
		public var targetUrl:String;
		
		private var _targetType:String;
		private var _xmlLoader:URLLoader;
		private var _swfLoader:Loader;
		private var _progressHandler:Function;
		private var _completeHandler:Function;
		private var _errorHandler:Function;
		private var _completeHandlerForQueue:Function;
		public function LoadingQueueItem(path:String)
		{
			//根据路径判断文件类型
			if(path.indexOf(".xml")!=-1)
			{
				_targetType = ITEM_TYPE_XML;
			}else if(path.indexOf(".swf")!=-1)
			{
				_targetType = ITEM_TYPE_SWF;
			}
			
			targetUrl = path;
		}
		/**
		 *开始加载 
		 * @param weakReference	 是否使用弱引用
		 * 
		 */		
		public function startLoad(weakReference:Boolean=false):void
		{
			var request:URLRequest = new URLRequest(targetUrl);
			if(_targetType==ITEM_TYPE_XML){
				loadXML(request,weakReference);
			}else{
				loadSWF(request,weakReference);
			}
		}
		
		/**
		 *注册加载进度回调处理 
		 * @param value
		 * 
		 */		
		public function registerProgressHandler(value:Function):void
		{
			this._progressHandler = value;	
		}
		/**
		 *注册加载完成回调处理 
		 * @param value
		 * 
		 */		
		public function registerCompleteHandler(value:Function):void
		{
			this._completeHandler = value;
		}
		/**
		 *注册错误回调处理 
		 * @param value
		 * 
		 */		
		public function registerErrorHandlerHandler(value:Function):void
		{
			this._errorHandler = value;	
		}
		
		/**
		 *注册队列完成处理 
		 * @param value
		 * 
		 */		
		public function registerCompleteHandlerForQueue(value:Function):void{
			this._completeHandlerForQueue = value;
		}
		
		/**
		 *加载XML文件 
		 * @param request		请求
		 * @param weakReference	是否弱引用
		 * 
		 */		
		private function loadXML(request:URLRequest,weakReference:Boolean=false):void{
			_xmlLoader = new URLLoader();
			_xmlLoader.addEventListener(Event.COMPLETE,this.onLoadComplete,false,0,!weakReference);
			if(_progressHandler!=null){
				_xmlLoader.addEventListener(ProgressEvent.PROGRESS,this.onLoadProgressHandle,false,0,!weakReference);
			}
			
			_xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,this.handleError,false,0,!weakReference);
			_xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.handleError,false,0,!weakReference);
			_xmlLoader.load(request);
		}
		/**
		 *加载SWF文件 
		 * @param request		请求
		 * @param weakReference	是否弱引用
		 * 
		 */		
		private function loadSWF(request:URLRequest,weakReference:Boolean=false):void{
			var loaderContext:LoaderContext = new LoaderContext();
			loaderContext.applicationDomain = ApplicationDomain.currentDomain;
			_swfLoader = new Loader();
			_swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoadComplete,false,0,!weakReference);
			if(_progressHandler!=null){
				_swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,this.onLoadProgressHandle,false,0,!weakReference);
			}
			
			_swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.handleError,false,0,!weakReference);
			_swfLoader.load(request,loaderContext);
		}
		
		/**
		 * 加载完成
		 * @param event
		 * 
		 */		
		private function onLoadComplete(event:Event):void{
			if(_completeHandler!=null&&(_completeHandler is Function)){
				_completeHandler(event);
			}
			
			if(_completeHandlerForQueue!=null&&(_completeHandlerForQueue is Function)){
				_completeHandlerForQueue(event);
			}
		}
		/**
		 *加载中... 
		 * @param event
		 * 
		 */		
		private function onLoadProgressHandle(event:ProgressEvent):void{
			if(_progressHandler!=null&&(_progressHandler is Function)){
				_progressHandler(event);
			}
		}
		/**
		 *错误处理 
		 * @param event
		 * 
		 */		
		private function handleError(event:Event):void
		{
			trace("error"+event);
			if(this._errorHandler!=null&&(this._errorHandler is Function)){
				this._errorHandler(event);
			}
		}

	}
}